package com.medicine.hbp.uitl;

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class DesUtil {
    private final static String DES = "DES";
    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs = new BASE64Encoder().encode(bt);
//        String strs = new Base64.encode(bt);
        return strs;
    }

    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,key.getBytes());
        return new String(bt);
    }

    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }


    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    /**
     * 加密解密
     * @param data 加密数据或解密数据
     * @param key  秘钥
     * @param lock TRUE加密或FALSE解密
     * @return
     */
    public static String crypt(String data, String key, boolean lock) {
        try {
            if (lock) {
                return encrypt(data, key).replaceAll("\\+", "%2b");
            } else {
                data = data.replaceAll("\\+", "%2b");
                return decrypt(data, key);
            }
        } catch (Exception e) {
            return "";
        }
    }

//    public static void main(String[] args) {
//    	MD5.getMD5String(str)
//    	String md5 = EncryUtils.getMD5String("1"+"20170112");
//    	System.out.println("md5加密:"+EncryUtils.getMD5String("1"+"20170112"));
//
//    	String encrypt_str = crypt("artId=1&date=20170112&token="+md5, "medicine", true);
//		System.out.println("加密:"+encrypt_str);
//
//		String url = crypt(encrypt_str, "medicine", false);
//		System.out.println("解密:"+url);
//
//		Map<String, String> map = StringUtils.getMapByregexUrl(url);
//		System.out.println("获取参数:"+map);
//
//		System.out.println("获取文章参数"+map.get("artId"));
//		System.out.println("获取时间参数"+map.get("date"));
//
//		String _md5 = EncryUtils.getMD5String(map.get("artId")+map.get("date"));
//
//		System.out.println("判断是否相等"+_md5.equals(md5));
//		
//	}
}